package com.chsi.onjava8.lambda;

import java.util.function.Function;

class I {
    @Override
    public String toString() { return "I"; }
}

class O {
    @Override
    public String toString() { return "O"; }
}

public class TransformFunction {
    static Function<I,O> transform(Function<I,O> in) {
        // f(x)=o -> { System.out.println(o); return o; }
        return in.andThen(o -> {
            System.out.println(o);
            return o;
        });
    }
    static Function<I,O> transform2(Function<I,O> in) {
        // f(x)=o -> { System.out.println(o); return o; }
        return in.compose(o -> {
            System.out.println(o);
            return o;
        });
    }
    public static void main(String[] args) {
        // g(x) = i -> {System.out.println(i); return new O(); }
        Function<I,O> f = transform(i -> {
            System.out.println(i);
            return new O();
        });
        // g(x) = i -> {System.out.println(i); return new O(); }
        Function<I,O> f2 = transform2(i -> {
            System.out.println(i);
            return new O();
        });
        // f(g(x))
        O o = f.apply(new I());
        System.out.println("-------------------------------------");
        // g(f(x))
        O o2 = f2.apply(new I());
    }
}
